package com.toidep.imageviewer;

import android.content.Context;
import android.graphics.RectF;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.imagezoom.ImageAttacher;
import com.imagezoom.ImageAttacher.OnMatrixChangedListener;
import com.imagezoom.ImageAttacher.OnPhotoTapListener;
import com.toidep.R;
import com.toidep.bitmaputil.ImageFetcher;
import com.toidep.config.ConfigUtility;
import com.toidep.interfaces.DialogListener;

public class ImageViewerAdapter extends PagerAdapter {

	private Context context;
	private String[] urls;
	public ImageFetcher mImageFetcher;

	public ImageViewerAdapter( String[] urls, Context context) {
		super();
		this.urls = urls;
		this.context = context;
	}

	@Override
	public int getCount() {
		return urls.length;
	}

	@Override
	public Object instantiateItem(View collection, int position) {
		String imageUrl = urls[position];
		String thumbsSize = "/thumbs/211x118";
		if (imageUrl.indexOf(thumbsSize) != -1)
			imageUrl = imageUrl.replace(thumbsSize, "");
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.layout_image_viewing, null);
		final LinearLayout layoutLoading = (LinearLayout) view
				.findViewById(R.id.layoutLoading);
		final ImageView mImaView = (ImageView) view
				.findViewById(R.id.imageview);
		layoutLoading.setVisibility(View.VISIBLE);
		mImageFetcher.loadImage(imageUrl, mImaView, null,
				ConfigUtility.scrWidth, ConfigUtility.scrHeight,
				new DialogListener() {

					@Override
					public void after() {
						layoutLoading.setVisibility(View.GONE);
						usingSimpleImage(mImaView);
					}
				});
		((ViewPager) collection).addView(view, 0);
		return view;
	}

	public void usingSimpleImage(ImageView imageView) {
		ImageAttacher mAttacher = new ImageAttacher(imageView);
		ImageAttacher.MAX_ZOOM = 4f; // Double the current Size
		ImageAttacher.MIN_ZOOM = 1f; // Half the current Size
		MatrixChangeListener mMaListener = new MatrixChangeListener();
		mAttacher.setOnMatrixChangeListener(mMaListener);

		PhotoTapListener mPhotoTap = new PhotoTapListener();
		mAttacher.setOnPhotoTapListener(mPhotoTap);
		mAttacher.setScaleType(ScaleType.FIT_CENTER);
	}

	private class PhotoTapListener implements OnPhotoTapListener {

		@Override
		public void onPhotoTap(View view, float x, float y) {
		}
	}

	private class MatrixChangeListener implements OnMatrixChangedListener {

		@Override
		public void onMatrixChanged(RectF rect) {
			;
		}
	}

	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}
}
